package org.eclipse.swt.snippets;

import java.util.Arrays;
import java.util.Comparator;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;

class SortListener implements Listener {

    private Table table;

    public void handleEvent(Event e) {
        int direction = table.getSortDirection();
        final boolean dir;
        if (direction == SWT.UP) {
            table.setSortDirection(SWT.DOWN);
            dir = false;
        } else {
            table.setSortDirection(SWT.UP);
            dir = true;
        }
        // final Collator collator = Collator.getInstance(Locale.getDefault());
        // TableColumn column = (TableColumn) e.widget;
        // TableColumn[] columns = table.getColumns();
        // int tmp = -1;
        // for (int i = 0; i < columns.length; i++) {
        // if (column == columns[i]) {
        // tmp = i;
        // break;
        // }
        // }
        // final int index = tmp;
        TableItem[] items = table.getItems();
        Comparator<TableItem> c = new Comparator<TableItem>() {

            @Override
            public int compare(TableItem o1, TableItem o2) {
                Message d1 = (Message) o1.getData();
                Message d2 = (Message) o2.getData();
                if (dir && d1.getDate().before(d2.getDate())) {
                    // setData(o1, d2);
                    // setData(o2, d1);
                }
                return 0;
            }
        };

        Arrays.sort(items, c);
        // table.setSortColumn(column);
    }
}